/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.controller;

import com.aptech.controller.util.JsfUtil;
import com.aptech.entity.ContactPerson;
import com.aptech.entity.RealEstate;
import com.aptech.session.CitiesFacadeLocal;
import com.aptech.session.ContactFacadeLocal;
import com.aptech.session.ContactPersonFacadeLocal;
import com.aptech.session.HouseTypeFacadeLocal;
import com.aptech.session.RealEstateFacadeLocal;
import com.aptech.session.RealEstateTypeFacadeLocal;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.SelectItem;
import org.apache.myfaces.custom.fileupload.UploadedFile;

/**
 *
 * @author DucMinh
 */
@ManagedBean
@RequestScoped
public class ControllerProperty {
    @EJB
    private CitiesFacadeLocal citiesFacade;
    @EJB
    private HouseTypeFacadeLocal houseTypeFacade;
    @EJB
    private RealEstateTypeFacadeLocal realEstateTypeFacade;

    @EJB
    private ContactPersonFacadeLocal contactPersonFacade;
    @EJB
    private ContactFacadeLocal contactFacade;
    @EJB
    private RealEstateFacadeLocal realEstateFacade;

    /** Creates a new instance of ControllerProperty */
    public ControllerProperty() {
    }
    private ContactPerson contact;
    private RealEstate realestate;
    private DataModel items = null;
    private UploadedFile uploadedFile;

    public ContactPerson getSelectedcontact() {
        if (contact == null) {
            contact = new ContactPerson();

        }
        return contact;
    }

    public RealEstate getrealestate() {
        if (realestate == null) {
            realestate = new RealEstate();

        }
        return realestate;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

   
   

    public RealEstate getRealestate() {
        return realestate;
    }

    public void setRealestate(RealEstate realestate) {
        this.realestate = realestate;
    }

    public String createAll() throws IOException {
        String fileName = uploadedFile.getName();
        String contentType = uploadedFile.getContentType();
        byte[] bytes = uploadedFile.getBytes();

        // save to upload directory
        String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("resources");
        FileOutputStream fos = new FileOutputStream(path + "/" + fileName);
        fos.write(bytes);
        fos.close();
        System.out.println("file saved to upload dir");

        // save news
        if (uploadedFile != null) {
            realestate.setImageURL(uploadedFile.getName());
        } else {
            realestate.setImageURL("Not available");
        }
        contactPersonFacade.create(contact);
        realestate.setContactPersonID(contact);
        realEstateFacade.create(realestate);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("RealEstateCreated"));
        
        return null;

    }
      public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(realEstateTypeFacade.findAll(), true);
    }
         public SelectItem[] getItemsAvailableSelectOne1() {
        return JsfUtil.getSelectItems(houseTypeFacade.findAll(), true);
    }
            public SelectItem[] getItemsAvailableSelectOne2() {
        return JsfUtil.getSelectItems(citiesFacade.findAll(), true);
    }

}
